﻿package com.tiankong.android.wallpaper;

import com.tiankong.android.wallpaper.bean.PhoneInfo;
import com.tiankong.android.wallpaper.bean.UserSetVisible;
import com.tiankong.android.wallpaper.draw.Drawer;

import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.preference.PreferenceManager;
import android.service.wallpaper.WallpaperService;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class LiveWallPaperService extends WallpaperService {

	private PhoneBroadcastReceiver broadcastReceiver = null;

	private IntentFilter filter = null;
	
	@Override
	public void onCreate() {
		super.onCreate();	
		receiverInit();
		setBaseSettingsValues();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		unregisterReceiver(broadcastReceiver);
	}

	public void setBaseSettingsValues() {

		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

		UserSetVisible.date_visible = (sharedPreferences.getBoolean(
				"date_visible", true));

		UserSetVisible.week_visible = (sharedPreferences.getBoolean(
				"week_visible", true));

		UserSetVisible.time_visible = (sharedPreferences.getBoolean(
				"time_visible", true));

		UserSetVisible.battery_visible = (sharedPreferences.getBoolean(
				"battery_visible", true));

		UserSetVisible.festivabg_visible = (sharedPreferences.getBoolean(
				"festivabg_visible", true));

		UserSetVisible.userdatebg_visible = (sharedPreferences.getBoolean(
				"userdatebg_visible", true));

		UserSetVisible.alarm_visible = (sharedPreferences.getBoolean(
				"alarm_visible", true));

	}

	public void receiverInit() {

		filter = new IntentFilter();

		filter.addAction(Intent.ACTION_BATTERY_CHANGED);
		filter.addAction(Intent.ACTION_BATTERY_LOW);
		filter.addAction(Intent.ACTION_BATTERY_OKAY);
		filter.addAction(Intent.ACTION_DATE_CHANGED);
		filter.addAction(Intent.ACTION_TIME_TICK);

		broadcastReceiver = new PhoneBroadcastReceiver();

		new PhoneInfo();
		registerReceiver(broadcastReceiver, filter);
	}

	@Override
	public Engine onCreateEngine() {
		// TODO Auto-generated method stub
		return new WallPaperEngine();
	}

	public class WallPaperEngine extends Engine {

		private LoopDrowHandler loopDrowHandler = null;

		public void onCreate(SurfaceHolder surfaceHolder) {
			super.onCreate(surfaceHolder);
			setTouchEventsEnabled(true);
			loopDrowHandler = new LoopDrowHandler();
		}

		@Override
		public void onVisibilityChanged(boolean visible) {
			if (visible) {
				loopDrowHandler.startDraw(new Drawer(getSurfaceHolder()));
			} else {
				loopDrowHandler.stopDraw();
			}
		}

		@Override
		public void onDestroy() {
			super.onDestroy();
			loopDrowHandler.stopDraw();
		}

		@Override
		public void onSurfaceCreated(SurfaceHolder holder) {
			super.onSurfaceCreated(holder);
			getScreenWH(holder);
			loopDrowHandler.startDraw(new Drawer(holder));
		}

		@Override
		public void onSurfaceChanged(SurfaceHolder holder, int format,
				int width, int height) {
			// TODO Auto-generated method stub
			super.onSurfaceChanged(holder, format, width, height);
			loopDrowHandler.stopDraw();
			getScreenWH(holder);
			loopDrowHandler.startDraw(new Drawer(holder));
		}

		@Override
		public void onSurfaceDestroyed(SurfaceHolder holder) {
			super.onSurfaceDestroyed(holder);
			loopDrowHandler.stopDraw();
		}

		@Override
		public void onTouchEvent(MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				
				// 触摸按下执行
//				SurfaceHolder surfaceHolder = getSurfaceHolder();
//				Canvas canvas = surfaceHolder.lockCanvas();
//				drawPoint(event.getX(), event.getY(), canvas);
//				surfaceHolder.unlockCanvasAndPost(canvas);
			}
			if (event.getAction() == MotionEvent.ACTION_UP) {
				// 触摸按下执行
//				SurfaceHolder surfaceHolder = getSurfaceHolder();
//				Canvas canvas = surfaceHolder.lockCanvas();
//				drawPoint(event.getX(), event.getY(), canvas);
//				surfaceHolder.unlockCanvasAndPost(canvas);
			}
		}

		private void drawPoint(float x, float y, Canvas canvas) {
			// 画墙纸);
			// canvas.save();
			// canvas.drawColor(Color.BLACK);

			Paint paint = null;

			paint = new Paint();
			paint.setColor(Color.RED);
			paint.setAntiAlias(true);
			paint.setStrokeWidth(4);
			paint.setStrokeCap(Paint.Cap.BUTT);
			paint.setStyle(Paint.Style.STROKE);
			paint.setTextSize(20);

			canvas.drawText((int) x + "," + (int) y, x, y, paint);
			// canvas.restore();

		}

		void getScreenWH(SurfaceHolder holder) {
			Canvas c = holder.lockCanvas();
			PhoneInfo.screenW = c.getWidth();
			PhoneInfo.screenH = c.getHeight();
			holder.unlockCanvasAndPost(c);
		}
	}

	

}
